clear
*******************************************************************************
* Read-in panel bloomberg data
use "./data/fomc_panel_b", clear

******CREATE VARIABLES FOR TABLE 9*****
foreach v in ex_rate_regime us_integ trade_open ka_open fin_depth dollar_exp dollar_debt vuln_index int_diff_3m int_diff_3m_chg {

*Step 1: Standardize each exposure variable, i.e. subtract mean and divide by std. dev.

	sum `v' 
	gen `v'_std = (`v' - r(mean)) / r(sd)
	
*Step 2: Construct a logistic transformation of the standardized variable

	gen `v'_log =  exp(`v'_std) / (1 + exp(`v'_std))
	
*Step 3: Re-center the logistic variable in terms of distance between 25th and 75th percentiles

	sum `v'_log, detail
	gen `v'_exposure = (`v'_log - r(p25)) / (r(p75) - r(p25))
	

*Step 4: Construct interaction term with shocks

	gen `v'_mps_int = `v'_exposure * mps_2day
	
	gen `v'_mpu_int = `v'_exposure * mpu_2day

}	 

*Drop U.S.
drop if ccode==49

*Keep emerging countries only
keep if advanced==0

********************************************************************************
*Loop Through And Calculate Coefficient for Every Grouping/Ordering*

foreach y in yield2y yield10y {

eststo clear
	
*Model Counter
local i = 1
	
	foreach a in fin_depth {

	foreach b in ka_open ex_rate_regime int_diff_3m_chg trade_open {
		 
	foreach c in ka_open ex_rate_regime int_diff_3m_chg trade_open {
			
	foreach d in ka_open ex_rate_regime int_diff_3m_chg trade_open {
				
	foreach e in ka_open ex_rate_regime int_diff_3m_chg trade_open { 

		preserve

		keep if `y'_2day!=. & `a'!=. & `b'!=. & `c'!=. & `d'!=. & `e'!=.
		
			*Sum of all variables, to not run models with duplicate variables
			egen tot_`a'= sum(`a')
			capture egen tot_`b'= sum(`b')
			capture egen tot_`c'= sum(`c')
			capture egen tot_`d'= sum(`d')
			capture egen tot_`e'= sum(`e')
				
			*First Variable
			quietly: reg `a'_mps_int mps_2day mpu_2day
			predict `a'_mps_orthog, residual

			quietly: reg  `a'_mpu_int mps_2day mpu_2day `a'_mps_orthog
			predict `a'_mpu_orthog, residual
			
			*Second Variable
			quietly: reg `b'_mps_int mps_2day mpu_2day `a'_mps_orthog `a'_mpu_orthog
			capture predict `b'_mps_orthog, residual

			quietly: reg  `b'_mpu_int mps_2day mpu_2day `a'_mps_orthog `a'_mpu_orthog `b'_mps_orthog
			capture predict `b'_mpu_orthog, residual
				
			*Third Variable
			quietly: reg `c'_mps_int mps_2day mpu_2day `a'_mps_orthog `a'_mpu_orthog `b'_mps_orthog `b'_mpu_orthog
			capture predict `c'_mps_orthog, residual

			quietly: reg  `c'_mpu_int mps_2day mpu_2day `a'_mps_orthog `a'_mpu_orthog `b'_mps_orthog `b'_mpu_orthog `c'_mps_orthog
			capture predict `c'_mpu_orthog, residual
			
			*Fourth Variable
			quietly: reg `d'_mps_int mps_2day mpu_2day `a'_mps_orthog `a'_mpu_orthog `b'_mps_orthog `b'_mpu_orthog `c'_mps_orthog `c'_mpu_orthog
			capture predict `d'_mps_orthog, residual

			quietly: reg  `d'_mpu_int mps_2day mpu_2day `a'_mps_orthog `a'_mpu_orthog `b'_mps_orthog `b'_mpu_orthog `c'_mps_orthog `c'_mpu_orthog `d'_mps_orthog
			capture predict `d'_mpu_orthog, residual
					
			*Fifth Variable
			quietly: reg `e'_mps_int mps_2day mpu_2day `a'_mps_orthog `a'_mpu_orthog `b'_mps_orthog `b'_mpu_orthog `c'_mps_orthog `c'_mpu_orthog `d'_mps_orthog `d'_mpu_orthog
			capture predict `e'_mps_orthog, residual

			quietly: reg  `e'_mpu_int mps_2day mpu_2day `a'_mps_orthog `a'_mpu_orthog `b'_mps_orthog `b'_mpu_orthog `c'_mps_orthog `c'_mpu_orthog `d'_mps_orthog `d'_mpu_orthog `e'_mps_orthog
			capture predict `e'_mpu_orthog, residual

			************* REGRESSIONS ***********************************
			*Baseline Ordering
			local mps_int "`a'_mps_orthog `b'_mps_orthog `c'_mps_orthog `d'_mps_orthog `e'_mps_orthog"
			local mpu_int "`a'_mpu_orthog `b'_mpu_orthog `c'_mpu_orthog `d'_mpu_orthog `e'_mpu_orthog"
			
			capture quietly: reghdfe `y'_2day_std mps_2day `mps_int' mpu_2day `mpu_int' if !(inlist(tot_`a',tot_`b',tot_`c',tot_`d',tot_`e')|inlist(tot_`b',tot_`a',tot_`c',tot_`d',tot_`e')|inlist(tot_`c',tot_`a',tot_`b',tot_`d',tot_`e')|inlist(tot_`d',tot_`a',tot_`b',tot_`c',tot_`e')|inlist(tot_`e',tot_`a',tot_`b',tot_`c',tot_`d')), noabsorb cluster(ccode time)
			
			capture eststo 
							
		restore
			
		
		local i = `i' + 1
		
		di `i'

	}
	}
	}
	}
	}
	
	*Coefficient Plots
	foreach var in ka_open {

		coefplot est*, keep(`var'_mpu_orthog) xline(0) ciopts(recast(rcap) lcolor(black)) levels(90) mcolor(white) mlcolor(black) msymbol(circle) sort(, descending) coeflabels(`var'_mpu_orthog=" ") grid(none) graphregion(col(white)) nokey
			
		graph export "tables\FigureA6_`y'.pdf", replace
	
	}


}
